package ru.amse.tsyganov.jumleditor.view.activity;

import java.util.Collections;
import java.util.List;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

import ru.amse.tsyganov.jumleditor.graphicaleditor.Coordinates;
import ru.amse.tsyganov.jumleditor.model.activity.ActionState;
import ru.amse.tsyganov.jumleditor.model.common.GraphModel;
import ru.amse.tsyganov.jumleditor.properties.EnumProperty;
import ru.amse.tsyganov.jumleditor.validator.Problem;
import ru.amse.tsyganov.jumleditor.view.ViewVisitor;
import ru.amse.tsyganov.jumleditor.view.common.GraphView;
import ru.amse.tsyganov.jumleditor.view.common.StateView;
import ru.amse.tsyganov.jumleditor.view.preferences.Colors;

public class ActionStateView<T extends ActionState> extends StateView<T> {
	
	private static final int HORIZONTAL_MARGIN = 10;
	private static final int VERTICAL_MARGIN = 5;
	private static final int RADIUS = 20;
	
	public ActionStateView(T model, 
			GraphView<? extends GraphModel> activityGraphView) {
		
		super(model, activityGraphView);
		initProperties();
	}
	
	@Override
	public void accept(ViewVisitor v) {
		v.visitActionStateView(this);
		visitTransitions(v);
	}
	
	@Override
	public void paintFigure(GC gc, Coordinates coordinates) {
		assert gc != null;
		
		gc.setBackground(Colors.getGreenColor());
		gc.fillRoundRectangle(
				coordinates.fromModelXToScreenX(getX()), 
				coordinates.fromModelYToScreenY(getY()), getWidth(), getHeight(), 
				RADIUS, RADIUS);
		
		
		gc.setForeground(getValidity().getColor());
		gc.setLineWidth(getValidity().getLineWidth());
		gc.drawRoundRectangle(
				coordinates.fromModelXToScreenX(getX() + getValidity().getLineWidth() / 2), 
				coordinates.fromModelYToScreenY(getY() + getValidity().getLineWidth() / 2), 
				getWidth() - getValidity().getLineWidth() / 2, 
				getHeight() - getValidity().getLineWidth() / 2, 
				RADIUS, RADIUS);
		
		String text = getModel().getName();
		Point extent = gc.stringExtent(text);
		
		gc.setForeground(Colors.getBlackColor());
		gc.setLineWidth(0);
		gc.drawText(text, 
				coordinates.fromModelXToScreenX(getX() + (getWidth() - extent.x) / 2), 
				coordinates.fromModelYToScreenY(getY() + (getHeight() - extent.y) / 2));
	}
	
	@Override
	public void prepaintEvent(GC gc, Coordinates coordinates) {
		super.prepaintEvent(gc, coordinates);
		
		String text = getModel().getName();
		
		Point extent = gc.stringExtent(text);
		
		if (getWidth() < extent.x + 2 * HORIZONTAL_MARGIN) {
			setWidth(extent.x + 2 * HORIZONTAL_MARGIN);
		}
		if (getHeight() < extent.y + 2 * VERTICAL_MARGIN) {
			setHeight(extent.y + 2 * VERTICAL_MARGIN);
		}
	}

	private void initProperties() {
		super.getProperties().addProperty(new EnumProperty("Call action mode",
				new String[] {
				ActionState.CallActionMode.SYNCHRONOUS.toString(),
				ActionState.CallActionMode.ASYNCHRONOUS.toString()
				}) {

			@Override
			public Integer getPropertyValue() {
				return getModel().getCallActionMode().ordinal();
			}

			@Override
			public void setPropertyValue(Integer newValue) {
				getModel().setCallActionMode(
					ActionState.CallActionMode.values()[newValue]);
			}
			
		});
	}
	
	@Override
	public List<Problem> getInnerProblems() {
		return Collections.emptyList();
	}

}
